const cuLog = {
  state: {
    homeCuLogList: [],

    runningLog: [],
    runningLogCount: 0,

    communicationLog: [],
    communicationLogCount: 0,

    callLog: [],
    callLogCount: 0,

    performanceLog: [],
    performanceLogCount: 0
  },
  mutations: {
    setHomeCuLogList (state, data) {
      state.homeCuLogList = data
    },
    pushEventLog (state, data) {
      let maxLength = 100
      state.homeCuLogList.unshift(data)
      if (state.homeCuLogList.length > maxLength) state.homeCuLogList.length = maxLength
    },

    setRunningLog (state, data) {
      state.runningLog = data
    },
    setRunningLogCount (state, data) {
      state.runningLogCount = data
    },
    setCommunicationLog (state, data) {
      state.communicationLog = data
    },
    setCommunicationLogCount (state, data) {
      state.communicationLogCount = data
    },
    setCallLog (state, data) {
      state.callLog = data
    },
    setCallLogCount (state, data) {
      state.callLogCount = data
    },
    setperformanceLog (state, data) {
      state.performanceLog = data
    },
    setperformanceLogCount (state, data) {
      state.performanceLogCount = data
    }
  },
  actions: {
    async getHomeCuLogList (context) {
      let response = await context.dispatch('axios', {
        method: 'get',
        url: '/api/abs/mtw/cu_log/?page_size=10'
      })
      context.commit('setHomeCuLogList', response.data.results)
    },
    async getRunningLogList (context, payload) {
      let response = await context.dispatch('axios', {
        method: 'get',
        url: '/api/abs/mtw/cu_log/',
        params: {
          page: payload.page,
          page_size: payload.page_size,

          begin: payload.begin,
          end: payload.end,
          msg_type: payload.msg_type
        }
      })
      context.commit('setRunningLog', response.data.results)
      context.commit('setRunningLogCount', response.data.count)
    },
    async getCommunicationLog (context, payload) {
      let response = await context.dispatch('axios', {
        method: 'get',
        url: '/api/abs/mtw/cu_communication_log/',
        params: {
          page: payload.page,
          page_size: payload.page_size,

          caller_device: payload.caller_device,
          called_device: payload.called_device,

          begin: payload.begin,
          end: payload.end
        }
      })
      context.commit('setCommunicationLog', response.data.results)
      context.commit('setCommunicationLogCount', response.data.count)
    },
    async getCallLog (context, payload) {
      let response = await context.dispatch('axios', {
        method: 'get',
        url: '/api/abs/mtw/call_log/',
        params: {
          page: payload.page,
          page_size: payload.page_size,

          caller: payload.caller,
          called: payload.called,

          begin: payload.begin,
          end: payload.end
        }
      })
      context.commit('setCallLog', response.data.results)
      context.commit('setCallLogCount', response.data.count)
    },
    async getPerformanceLog (context, payload) {
      let response = await context.dispatch('axios', {
        method: 'get',
        url: '/api/abs/mtw/freq_usage/',
        params: {
          page: payload.page,
          page_size: payload.page_size,

          begin: payload.begin,
          end: payload.end
        }
      })
      context.commit('setperformanceLog', response.data.results)
      context.commit('setperformanceLogCount', response.data.count)
    }
  },
  getters: {
    getHomeCuLogList: (state) => {
      return state.homeCuLogList.map(item => {
        let data
        try {
          data = JSON.parse(item.event_data)
        } catch (e) {
          data = {}
        }
        return {
          ...item,
          dateTime: item.time.split('.')[0].split('T').join(' '),
          data
        }
      })
    },
    getRunningLogList: (state) => {
      return state.runningLog.map(item => {
        return {
          ...item,
          dateTime: item.time.split('.')[0].split('T').join(' '),
          data: JSON.parse(item.event_data)
        }
      })
    },
    getCommunicationLogList: state => {
      return state.communicationLog.map(item => {
        return {
          ...item,
          startTime: item.start_time.split('.')[0].split('T').join(' '),
          endTime: item.time.split('.')[0].split('T').join(' ')
        }
      })
    },
    getCallLogList: state => {
      return state.callLog.map(item => {
        return {
          ...item,
          time: item.time.split('.')[0].split('T').join(' '),
          resultColor: item.result ? '#5e88c2' : '#F56C6C'
        }
      })
    },
    getPerformanceLogList: state => {
      return state.performanceLog.map(item => {
        return {
          ...item,
          time: item.time.split('.')[0].split('T').join(' '),
          resultColor: item.result ? '#5e88c2' : '#F56C6C',
          tdm_rate: item.tdm_rate / 10,
          aloha_rate: item.aloha_rate / 10,
          trfc_rate: item.trfc_rate / 10
        }
      })
    }
  }
}
export default cuLog
